package top.baselitch.liteorm;

import android.database.sqlite.SQLiteDatabase;

import java.util.HashSet;
import java.util.Set;

import top.baselitch.liteorm.annotation.Table;

public class Tables {
    private static Set<TableModel> tableModels = new HashSet<>();
    public static <T> void addTable(Class<T> tableModelClass){
        if(!isExist(tableModelClass)){
            tableModels.add(new TableModel.Builder(tableModelClass).builder());
        }
    }
    public static void create(SQLiteDatabase db){
        for(TableModel tableModel : tableModels){
            String sql = tableModel.buildCreateSql();
            db.execSQL(sql);
        }
    }
    private static <T> boolean isExist(Class<T> tableModelClass){
        Table table = tableModelClass.getAnnotation(Table.class);
        for(TableModel tm : tableModels){
            if(tm.getTableName().equals(table.value())){
                return true;
            }
        }
        return false;
    }
}
